<?php

namespace App\Http\Controllers\AppApi\Home;

use App\admin\Service\OA;
use App\Http\Controllers\AppApi\BaseController;
use App\Models\Admin\CompanyModel;
use App\Models\Main\UserScoreModel;
use App\Repositories\CommonRepository;
use App\Repositories\Company\CompanyRepository;
use App\Repositories\JobsRepository;
use App\Repositories\StaffRepository;
use App\Services\Admin\JobCompanyService;
use App\Services\Admin\CompanyService;
use App\Repositories\UserRepository;
use App\Services\UserService;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    private $companyRepository;
    private $jobsRepository;
    private $userRepository;
    private $commonRepository;
    private $jobCom;
    private $com;
    private $staffRepository;

    public function __construct(
        CompanyRepository $companyRepository,
        JobsRepository $jobsRepository,
        UserRepository $userRepository,
        CommonRepository $commonRepository,
        StaffRepository $staffRepository
    )
    {
        parent::__construct();
        $this->companyRepository = $companyRepository;
        $this->jobsRepository = $jobsRepository;
        $this->userRepository = $userRepository;
        $this->commonRepository = $commonRepository;
        $this->jobCom = new JobCompanyService();
        $this->com = new CompanyService();
        $this->staffRepository = $staffRepository;
    }

    /**
     * /api/home/index?
     */
    public function index(Request $request)
    {
        // exit('ddd');
        //根据经纬度获取附近的企业IDs
//        $lat = trim($request->input('lat',30.285291));
//        $lng = trim($request->input('lng',120.135685));
//        $distance = 0.5; //范围，单位千米
//        $cids = $this->companyRepository->getCompanyByLatLng($lat,$lng,$distance);
        //获取推荐岗位,前期按时间排序
//        $jobs = $this->jobsRepository->getJobs($cids);


        $value = \Illuminate\Support\Facades\Cache::remember('/api/home/index', 1000, function () {
            $fabu = CompanyModel::infoexists(OA::$user->getId());////如果存在企业信息,则允许发布
            $jobs = $this->jobCom->getAppJobComs();
            //获取推荐工厂
//        $factorys = $this->companyRepository->getCompanyByIds($cids);
            $factorys = $this->com->getAppFacs();
            return [
                'jobs' => $jobs,
                'factorys' => $factorys,
                'fabu' => $fabu
            ];
        });

        return $this->output($value);
    }

    public function getSearch()
    {
        $keywords = $this->userRepository->getUserSearchKeywords($this->uid);
        return $this->output(['keywords' => $keywords]);
    }

    public function saveSearch(Request $request)
    {
        $keyword = trim($request->input('keyword', ''));
        if (!$keyword) {
            return $this->output([], '搜索词不能为空', 500);
        }

        if ($rid = $this->userRepository->addUserSearchKeywords($this->uid, $this->username, $keyword)) {
            return $this->output($rid);
        }

        return $this->output([], '保存失败', 500);
    }

    public function getLocation()
    {
        $provinces = $this->commonRepository->getProvinces();
        foreach ($provinces as &$province) {
            $province['citys'] = $this->commonRepository->getcitys($province['id']);
            foreach ($province['citys'] as &$city) {
                $city['area'] = $this->commonRepository->getAreas($city['id']);
            }
        }
        $json_string = json_encode($provinces);
        file_put_contents('location.json', $json_string);
    }

    public function getProvince()
    {
        $provinces = $this->commonRepository->getProvinces();
        return $this->output(['province' => $provinces]);
    }

    public function getCitys(Request $request)
    {
        $id = intval($request->input('id', 0));
        $citys = $this->commonRepository->getCitys($id);
        return $this->output(['citys' => $citys]);
    }

    public function getArea(Request $request)
    {
        $id = intval($request->input('id', 0));
        $area = $this->commonRepository->getAreas($id);
        return $this->output(['area' => $area]);
    }

    /**
     * 保存首页的获取的地理位置
     */
    public function saveAddress(Request $request)
    {
        $param = $request->input();
        $rule = [
            'lng' => 'required|float',  //纬度
            'lat' => 'required|float'//经度
        ];
        $this->validate($param, $rule);
        $user_id = UserService::$user['uid'];
        $user = $this->userRepository->getUserById($user_id);
        $this->userRepository->update($user, $param);
        return $this->output();
    }

    //
    public function searchDetail(Request $request)
    {
        $param = $request->input();
        $rule = [
            'keyword' => 'required',  //关键字
            'type' => 'required|integer'
        ];
        $this->validate($request, $rule);

        if ($param['type'] == 1) {
//            $this->
        }

    }

    //找客户结果
    /// /api/home/customer?cate=2
    public function customer(Request $request)
    {

        $param = $request->input();
        $rule = [
            'cate' => 'required'
        ];
        $this->validate($request, $rule);

        $data = $this->companyRepository->getAllCompanyByCate($param['cate']);

        return $this->output($data);
    }

    //客户分类
    public function customer_cate()
    {
        $data = $this->companyRepository->customerCate();
        return $this->output($data);
    }

    //找工厂
    public function factory(Request $request)
    {
        $param = $request->input();
        $rule = [
            'cate' => 'required'
        ];
        $this->validate($request, $rule);

        $data = $this->companyRepository->getAllCompanyByCate($param['cate']);

        return $this->output($data);
    }

    //工厂类型列表
    public function factory_cate()
    {
        $data = $this->companyRepository->getCompanyCagetory(1);
        return $this->output($data);
    }


    //找员工职位职位列表
    public function staff_cate(Request $request)
    {
        $type = $request->type;
        if ($type == 3) {
            $genre = 1;
        } else if (in_array($type, [2, 4])) {
            $genre = 2;
        } else {
            return $this->toJson(-2);
        }
        $job = $this->jobsRepository->getDepartment(['id', 'name'], $genre);
        return $this->output($job);
    }

    //获取部门下员工
    ////api/home/staff
    public function staff(Request $request)
    {
        $param = $request->input();
        $rule = [
            'cate' => 'required'
        ];
        $this->validate($request, $rule);

        $staff = $this->jobsRepository->getDepartmentStaff($param['cate']);

        return $this->output($staff);
    }

    //员工详情
    //  /api/home/staff/detail?
    public function staff_detail(Request $request)
    {

        $param = $request->input();
        $rule = [
            'id' => 'required'
        ];
        $this->validate($request, $rule);

        $data = $this->staffRepository->staff_detail($param['id']);
        return $this->output($data);
    }

    //客户详情
    /// /api/home/customer/detail?id=2
    public function customer_detail(Request $request)
    {

        //产品、服务、关于公司（暂时隐藏）
        $param = $request->input();
        $rule = [
            'id' => 'required'
        ];
        $this->validate($request, $rule);

        $data = $this->companyRepository->companyDetail($param['id']);

        return $this->output($data);
    }

    //工厂详情
    public function factory_detail(Request $request)
    {
        $param = $request->input();
        $rule = [
            'id' => 'required'
        ];
        $this->validate($request, $rule);


    }
}